package com.artemchep.keyguard.feature.home.settings.component

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.LocalMinimumInteractiveComponentSize
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import arrow.core.partially1
import com.artemchep.keyguard.common.io.launchIn
import com.artemchep.keyguard.common.usecase.GetCheckPwnedPasswords
import com.artemchep.keyguard.common.usecase.PutCheckPwnedPasswords
import com.artemchep.keyguard.common.usecase.WindowCoroutineScope
import com.artemchep.keyguard.feature.home.settings.LocalSettingItemShape
import com.artemchep.keyguard.feature.home.vault.component.FlatItemSimpleExpressive
import com.artemchep.keyguard.res.Res
import com.artemchep.keyguard.res.*
import com.artemchep.keyguard.ui.FlatItem
import com.artemchep.keyguard.ui.poweredby.PoweredBy2factorauth
import com.artemchep.keyguard.ui.poweredby.PoweredByHaveibeenpwned
import com.artemchep.keyguard.ui.theme.Dimens
import com.artemchep.keyguard.ui.theme.horizontalPaddingHalf
import org.jetbrains.compose.resources.stringResource
import kotlinx.coroutines.flow.map
import org.kodein.di.DirectDI
import org.kodein.di.instance

fun settingCheckPwnedPasswordsProvider(
    directDI: DirectDI,
) = settingCheckPwnedPasswordsProvider(
    getCheckPwnedPasswords = directDI.instance(),
    putCheckPwnedPasswords = directDI.instance(),
    windowCoroutineScope = directDI.instance(),
)

fun settingCheckPwnedPasswordsProvider(
    getCheckPwnedPasswords: GetCheckPwnedPasswords,
    putCheckPwnedPasswords: PutCheckPwnedPasswords,
    windowCoroutineScope: WindowCoroutineScope,
): SettingComponent = getCheckPwnedPasswords().map { checkPwnedPasswords ->
    val onCheckedChange = { shouldCheckPwnedPasswords: Boolean ->
        putCheckPwnedPasswords(shouldCheckPwnedPasswords)
            .launchIn(windowCoroutineScope)
        Unit
    }

    SettingIi {
        SettingCheckPwnedPasswords(
            checked = checkPwnedPasswords,
            onCheckedChange = onCheckedChange,
        )
    }
}

@Composable
private fun SettingCheckPwnedPasswords(
    checked: Boolean,
    onCheckedChange: ((Boolean) -> Unit)?,
) {
    FlatItemSimpleExpressive(
        shapeState = LocalSettingItemShape.current,
        trailing = {
            CompositionLocalProvider(
                LocalMinimumInteractiveComponentSize provides Dp.Unspecified,
            ) {
                Switch(
                    checked = checked,
                    enabled = onCheckedChange != null,
                    onCheckedChange = onCheckedChange,
                )
            }
        },
        title = {
            Text(
                text = stringResource(Res.string.pref_item_check_pwned_passwords_title),
            )
        },
        text = {
            val text = stringResource(Res.string.watchtower_item_pwned_passwords_text)
            Text(text)
        },
        footer = {
            PoweredByHaveibeenpwned(
                modifier = Modifier
                    .padding(
                        horizontal = Dimens.contentPadding,
                        vertical = 4.dp,
                    ),
            )
        },
        onClick = onCheckedChange?.partially1(!checked),
    )
}
